package pl.edu.mimuw.irs.rcp.controller.tester;

import org.eclipse.core.expressions.PropertyTester;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.gef.commands.CommandStack;

public class CommandStackTester extends PropertyTester {
	
	// *** Properties

	private static final String IS_DIRTY = "isDirty";
	private static final String CAN_UNDO = "canUndo";
	private static final String CAN_REDO = "canRedo";

	// *** Methods
	
	@Override
	public boolean test(Object receiver, String property, Object[] args,
			Object expectedValue) {
		if (!(receiver instanceof IAdaptable)) return false;
		IAdaptable adaptable = (IAdaptable) receiver;
		CommandStack stack = (CommandStack) adaptable.getAdapter(CommandStack.class);
		if (stack == null) return false;

		boolean retval = false;
		if (IS_DIRTY.equals(property)) retval = stack.isDirty();
		else if (CAN_UNDO.equals(property)) retval = stack.canUndo();
		else if (CAN_REDO.equals(property)) retval = stack.canRedo();
		
		return retval;
	}
}
